package org.jetnuke.dao.sqlserver;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.List;

import net.jforum.JForumExecutionContext;
import net.jforum.util.preferences.SystemGlobals;

public class SqlServerDownloadDownloadDAO extends
		org.jetnuke.dao.generic.GenericDownloadDownloadDAO {
	
	public List search(String title, String description, int offset, int rowcount) throws Exception {
		PreparedStatement p;
		String q = SystemGlobals.getSql("DownloadDownloadModel.search");
		q=q.replaceAll("#start",String.valueOf(offset));
		q=q.replaceAll("#limit",String.valueOf(rowcount));
		p = JForumExecutionContext.getConnection().prepareStatement(q);
		p.setString(1,title);
		p.setString(2,description);
		p.setString(3,title);
		p.setString(4,description);
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();
		
		return list;
	}
	public List selectHitsAsc(int cid, int start, int limit) throws Exception {
		PreparedStatement p;
		String q = SystemGlobals.getSql("DownloadDownloadModel.selectHitsAsc");
		q=q.replaceAll("#start",String.valueOf(start));
		q=q.replaceAll("#limit",String.valueOf(limit));
		p = JForumExecutionContext.getConnection().prepareStatement(q);
		p.setInt(1,cid);
		p.setInt(2,cid);
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();
		
		return list;
	}
	public List selectHitsDesc(int cid, int start, int limit) throws Exception {
		PreparedStatement p;
		String q = SystemGlobals.getSql("DownloadDownloadModel.selectHitsDesc");
		q=q.replaceAll("#start",String.valueOf(start));
		q=q.replaceAll("#limit",String.valueOf(limit));
		p =JForumExecutionContext.getConnection().prepareStatement(q);
		p.setInt(1,cid);
		p.setInt(2,cid);
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();
		
		return list;
	}
	public List selectRatingAsc(int cid, int start, int limit)
			throws Exception {
		PreparedStatement p;
		String q = SystemGlobals.getSql("DownloadDownloadModel.selectRatingAsc");
		q=q.replaceAll("#start",String.valueOf(start));
		q=q.replaceAll("#limit",String.valueOf(limit));
		p = JForumExecutionContext.getConnection().prepareStatement(q);
		p.setInt(1,cid);
		p.setInt(2,cid);
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();
		
		return list;
	}
	public List selectRatingDesc(int cid, int start, int limit)
			throws Exception {
		PreparedStatement p;
		String q = SystemGlobals.getSql("DownloadDownloadModel.selectRatingDesc");
		q=q.replaceAll("#start",String.valueOf(start));
		q=q.replaceAll("#limit",String.valueOf(limit));
		p = JForumExecutionContext.getConnection().prepareStatement(q);
		p.setInt(1,cid);
		p.setInt(2,cid);
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();
		
		return list;
	}
	public List selectDateAsc(int cid, int start, int limit) throws Exception {
		PreparedStatement p;
		String q2 = SystemGlobals.getSql("DownloadDownloadModel.selectDateAsc");
		q2=q2.replaceAll("#start",String.valueOf(start));
		q2=q2.replaceAll("#limit",String.valueOf(limit));
		p = JForumExecutionContext.getConnection().prepareStatement(q2);
		p.setInt(1,cid);
		p.setInt(2,cid);
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();
		
		return list;
	}
	public List selectDateDesc(int cid, int start, int limit) throws Exception {
		PreparedStatement p;
		String q = SystemGlobals.getSql("DownloadDownloadModel.selectDateDesc");
		q=q.replaceAll("#start",String.valueOf(start));
		q=q.replaceAll("#limit",String.valueOf(limit));
		p = JForumExecutionContext.getConnection().prepareStatement(q);
		p.setInt(1,cid);
		p.setInt(2,cid);
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();
		
		return list;
	}
	public List selectTopDown(int limit) throws Exception {
		PreparedStatement p;
		
		String q1 = SystemGlobals.getSql("GenericModel.selectByLimit");
		String q2 = SystemGlobals.getSql("DownloadDownloadModel.selectTopDown");
		p = JForumExecutionContext.getConnection().prepareStatement(q1+" "+limit+" "+q2);
		
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();
		
		return list;
	}
	public List selectWithRating(int totalVotesMin, int limit) throws Exception {
		PreparedStatement p;
		
		String q1 = SystemGlobals.getSql("GenericModel.selectByLimit");
		String q2 = SystemGlobals.getSql("DownloadDownloadModel.selectWithRating");
		p = JForumExecutionContext.getConnection().prepareStatement(q1+" "+limit+" "+q2);
		p.setInt(1,totalVotesMin);
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();
		
		return list;
	}
	public List getByHitsDown(int start, int limit) throws Exception {
		PreparedStatement p;
		String q2 = SystemGlobals.getSql("DownloadDownloadModel.selectByHitsDown");
		q2=q2.replaceAll("#start",String.valueOf(start));
		q2=q2.replaceAll("#limit",String.valueOf(limit));
		p = JForumExecutionContext.getConnection().prepareStatement(q2);
		//p.setInt(1,totalVotesMin);
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();
		
		return list;
	}
	public List selectTitleAsc(int cid, int start, int limit) throws Exception {
		PreparedStatement p;
		String q2 = SystemGlobals.getSql("DownloadDownloadModel.selectTitleAsc");
		q2=q2.replaceAll("#start",String.valueOf(start));
		q2=q2.replaceAll("#limit",String.valueOf(limit));
		p = JForumExecutionContext.getConnection().prepareStatement(q2);
		p.setInt(1,cid);
		p.setInt(2,cid);
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();
		
		return list;
	}
	public List selectTitleDesc(int cid, int start, int limit)
			throws Exception {
		PreparedStatement p;
		String q = SystemGlobals.getSql("DownloadDownloadModel.selectTitleDesc");
		q=q.replaceAll("#start",String.valueOf(start));
		q=q.replaceAll("#limit",String.valueOf(limit));
		p =JForumExecutionContext.getConnection().prepareStatement(q);
		p.setInt(1,cid);
		p.setInt(2,cid);
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();
		
		return list;
	}
}
